/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function validateManager(form){
    //alert("sdg"+form['email'].value);
    var flag = 0;
    var regPass = /^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=[^\W]*\W)(?=.*[\d\W]).*$/;
    var regTelNo = /\b[0-9]{7,20}\b/;
    var regEmail = /\b[A-z0-9._]+@\b[A-z0-9.-]+\b.\b[A-z]+\b/;
    var regPostal = /\b[0-9]{3,10}\b/;
    var regExpiration = /\b[0-9]{1,4}\b/;
    
    document.getElementById("nameError").innerHTML = "";
    document.getElementById("userError").innerHTML = "";
    document.getElementById("passError").innerHTML = "";
    document.getElementById("emailError").innerHTML = "";
    document.getElementById("TelNoError").innerHTML = "";
    document.getElementById("houseError").innerHTML = "";
    document.getElementById("streetError").innerHTML = "";
    document.getElementById("subError").innerHTML = "";
    document.getElementById("cityError").innerHTML = "";
    document.getElementById("postalError").innerHTML = "";
    document.getElementById("countryError").innerHTML = "";
    
    if(form['nameFirst'].value.length === 0 || form['nameLast'].value.length === 0){
        flag += 1;
        document.getElementById("nameError").innerHTML = "*Please enter your full name.";
    }  
    if(form['username'].value.length === 0 || (form['username'].value.length <= 7 && form['username'].value.length >= 16)){
        flag += 1;
        document.getElementById("userError").innerHTML = "*Username must be 8 - 15 characters in length.";
    } 
    if(form['password'].value.length === 0 || !document.forms["registrationForm"]["password"].value.match(regPass)){
        document.getElementById("passError").innerHTML = "*Password must be at least 8 characters and must contain at least 1 uppercase letter, 1 number, and 1 special character.";
        flag+=1;    
    }
    else if(form['password'].value !== form['retypePassword'].value){
        document.getElementById("passError").innerHTML = "*Passwords did not match.";
        flag+=1;
    }
    if(form['telephone'].value.length === 0 || (!document.forms["registrationForm"]["telephone"].value.match(regTelNo))){
        document.getElementById("TelNoError").innerHTML = "*Please enter a valid telephone number.";
        flag+=1;
    }
    if(form['email'].value.length === 0 || !document.forms["registrationForm"]["email"].value.match(regEmail)){
        document.getElementById("emailError").innerHTML = "*Please enter a valid e-mail address.";
        flag+=1;
    }
    if(form['AddressDeliveryNumber'].value.length === 0 || !document.forms["registrationForm"]["AddressDeliveryNumber"].value.match(regExpiration)){
        flag += 1;
        document.getElementById("houseError").innerHTML = "*This field is required.";
    }    
    if(form['AddressDeliveryStreet'].value.length === 0){
        flag += 1;
        document.getElementById("streetError").innerHTML = "*This field is required.";
    }    
    if(form['AddressDeliverySubdivision'].value.length === 0){
        flag += 1;
        document.getElementById("subError").innerHTML = "*This field is required.";
    }  
    if(form['AddressDeliveryCity'].value.length === 0){
        flag += 1;
        document.getElementById("cityError").innerHTML = "*This field is required.";
    }  
    if(form['AddressDeliveryPostal'].value.length === 0 || !document.forms["registrationForm"]["AddressDeliveryPostal"].value.match(regPostal)){
        document.getElementById("postalError").innerHTML = "*Invalid postal code.";
        flag+=1;
    }
    if(form['AddressDeliveryCountry'].value.length === 0){
        flag += 1;
        document.getElementById("countryError").innerHTML = "*This field is required.";
    }  
    
    
    if(flag >= 1){
        alert("Please double check your information.");
        return false;
    }
    else{
        form.action="managerServlet";
        return true;
    }
}
